package com.cyl.h5.service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.act.domain.entity.CouponActivity;
import com.cyl.manager.act.domain.query.CouponActivityQuery;
import com.cyl.manager.act.mapper.CouponActivityMapper;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 优惠券活动表Service业务层处理
 *
 * @author zcc
 */
@Service
public class H5CouponActivityService {
    @Autowired
    private CouponActivityMapper couponActivityMapper;
    
    /**
     * 	查询总数
     *
     * @param id 优惠券活动表主键
     */
    public CouponActivity selectCountByActivityId(Long id) {
        couponActivityMapper.receiveCoupon(id);
        return couponActivityMapper.selectById(id);
    }
    
    
    /**
     * 查询优惠券活动表列表
     * deptDpId
     * @param query 查询条件
     * @param page  分页条件
     * @return 优惠券活动表
     */
    public List<CouponActivity> selectDpActivityList(CouponActivityQuery query) {
        LocalDateTime now = LocalDateTime.now();
        PageHelper.startPage(1, 10,"left_count desc, create_time desc");
        QueryWrapper<CouponActivity> qw = new QueryWrapper<>();
        qw.eq("dept_dp_id", query.getDeptDpId());
        qw.lt("begin_time", now);
        qw.gt("end_time", now);
        List<CouponActivity> list = couponActivityMapper.selectList(qw);
        if(list!=null) {
        	for (CouponActivity couponActivity : list) {
        		couponActivityMapper.receiveCoupon(couponActivity.getId());
			}
        }
        return  couponActivityMapper.selectList(qw);
    }
 
    

}
